package com.corewillsoft.fireflies.engine;

import java.util.Set;

import com.corewillsoft.fireflies.gameobjects.Firefly;
import com.corewillsoft.fireflies.storage.DbManager;

public class ResultChecker {

	private static final int REQUIRED_COUNT = 5;

	public static LevelResult checkResult(Set<Firefly> fireflies, int currentLevel) {
		if ((fireflies.size() - REQUIRED_COUNT) <= getNumberOfSaved(fireflies)) {
			if (((new DbManager()).getLastLevelId() == currentLevel)) {
				return LevelResult.GAME_FINISHED;
			}
			return LevelResult.WON;
		} else {
			return LevelResult.LOST;
		}
	}

	private static int getNumberOfSaved(Set<Firefly> fireflies) {
		int result = 0;
		for (Firefly firefly : fireflies) {
			if (firefly.isSaved()) {
				result++;
			}
		}
		return result;
	}
}
